/*
 * ! Ext JS Library 3.1.1 Copyright(c) 2006-2010 Ext JS, LLC licensing@extjs.com
 * http://www.extjs.com/license
 */
Ext.ns('Ext.ux.tree');

/**
 * @class Ext.ux.tree.XmlTreeLoader
 * @extends Ext.tree.TreeLoader
 *          <p>
 *          A TreeLoader that can convert an XML document into a hierarchy of
 *          {@link Ext.tree.TreeNode}s. Any text value included as a text node
 *          in the XML will be added to the parent node as an attribute called
 *          <tt>innerText</tt>. Also, the tag name of each XML node will be
 *          added to the tree node as an attribute called <tt>tagName</tt>.
 *          </p>
 *          <p>
 *          By default, this class expects that your source XML will provide the
 *          necessary attributes on each node as expected by the
 *          {@link Ext.tree.TreePanel} to display and load properly. However,
 *          you can provide your own custom processing of node attributes by
 *          overriding the {@link #processNode} method and modifying the
 *          attributes as needed before they are used to create the associated
 *          TreeNode.
 *          </p>
 * @constructor Creates a new XmlTreeloader.
 * @param {Object}
 *           config A config object containing config properties.
 */
Ext.ux.tree.XmlTreeLoader = Ext.extend(Ext.tree.TreeLoader, {
			/**
			 * @property XML_NODE_ELEMENT XML element node (value 1, read-only)
			 * @type Number
			 */
			XML_NODE_ELEMENT : 1,

			/**
			 * @property XML_NODE_TEXT XML text node (value 3, read-only)
			 * @type Number
			 */
			XML_NODE_TEXT : 3,

			xmlData : null,

			el : null,

			load : function(node, callback, scope)
			{
				this.constructor.superclass.load(node, callback, scope);

				// convert the string to an XML object
				if (window.DOMParser)
				{
					this.xmlData = (new DOMParser()).parseFromString(Ext.get(this.el).dom.innerHTML, "text/xml");
				} else
				// Internet Explorer
				{
					this.xmlData = new ActiveXObject("Microsoft.XMLDOM");
					this.xmlData.async = "false";
					this.xmlData.loadXML(Ext.get(this.el).dom.innerHTML);
				}

				var root = this.xmlData.firstChild;

				node.beginUpdate();
				node.appendChild(this.parseXml(root));
				node.endUpdate();
				
				return node;
			},

			// private
			parseXml : function(formField)
			{
				var nodes = [];
				var treeNode = null;

				Ext.each(formField.childNodes, function(compositeField)
						{
							Ext.each(compositeField.childNodes, function(n)
									{
										if (n.nodeName.toLowerCase() == "question")
										{
											treeNode = this.createNode(n);

											nodes.push(treeNode);
										} else if (n.nodeName.toLowerCase() == "formfield")
										{
											var child = this.parseXml(n);

											treeNode.appendChild(child);

											nodes.push(treeNode);
										}
									}, this);
						}, this);

				return nodes;
			},

			// private override
			createNode : function(node)
			{
				var attr = {
					tagName : node.tagName.toLowerCase(),
					text : node.textContent || node.text,
					loaded : true,
					expanded : true
				};

				return Ext.ux.tree.XmlTreeLoader.superclass.createNode.call(this, attr);
			}
		});

// backwards compat
Ext.ux.XmlTreeLoader = Ext.ux.tree.XmlTreeLoader;
